function moveCursorToNextLine(editor, line_inc=1) {
    var position = editor.getPosition();
    var newPosition = {
        lineNumber: position.lineNumber + line_inc,
        column: position.column
    };
    editor.setPosition(newPosition);
}

function moveToLineEnd(editor) {
    editor.setPosition({
        lineNumber: editor.getPosition().lineNumber,
        column: editor.getModel().getLineLength(editor.getPosition().lineNumber)+1
    });    
}

function countLeadingSpaces(line) {
  var matches = line.match(/^\s*/);
  if (matches && matches[0]) {
    return matches[0].length;
  }
  return 0;
}

const onkeydown = (e, editor) => {
    var model = editor.getModel();
    var position = editor.getPosition();
    var lineContent = model.getLineContent(position.lineNumber);
    var lineLength = lineContent.length+1;
    var isList = lineContent.trim().startsWith('-');
    let ln = position.lineNumber
    const spc_cnt = countLeadingSpaces(lineContent)

    if (e.keyCode === monaco.KeyCode.Enter && !e.shiftKey) {
        if (isList) {
            if (position.column === lineLength) {
                let newLineContent = ' '.repeat(spc_cnt) + '- \n'
                print('spc_cnt', spc_cnt)
                ln = ln+1
                var edits = [{
                    range: new monaco.Range(ln, 1, ln, 1),
                    text: newLineContent,
                    forceMoveMarkers: false
                }];
                editor.executeEdits('api', edits);
                moveCursorToNextLine(editor);

                e.preventDefault();
            }
        }
    }

    if (e.keyCode === monaco.KeyCode.Tab) {
      print('is Tab')

      if (isList) {
        const indent = '  ';
        let newLineContent = lineContent
        
        if(e.shiftKey){
            newLineContent = newLineContent.replace(/^ {2}/, '')
        }else{
            newLineContent = indent + lineContent
        }
        print('newLineContent', newLineContent)

        // newLineContent = newLineContent.trim()
        newLineContent += '\n'

        var edits = [{
            range: new monaco.Range(ln, 1, ln+1, 1),
            text: newLineContent,
            forceMoveMarkers: false
        }];
        editor.executeEdits('api', edits);


        if(e.shiftKey){
            moveCursorToNextLine(editor, -1);
        }
        moveToLineEnd(editor)
        e.preventDefault();
      }
    }        
}

export const initMonaco =  (elid, vspath, doc, lang, onCreate, onChange) => {
    require.config({ paths: { 'vs': vspath }});
    require(['vs/editor/editor.main'], function() {
        var editor = monaco.editor.create(document.getElementById(elid), {
            value: doc,
            language: lang
        });

        onCreate(editor)
        // updateMd(init_doc)

        editor.onDidChangeModelContent(function(event) {
                //console.log('Model content changed:', event);
            const doc = editor.getValue()
            onChange(doc)
        });

        editor.onKeyDown(function (e) {
            onkeydown(e, editor)
        });

    });
};